﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    class Validation
    {
        public static bool IsOnTheField(int X, int Y, GameField field, List<Node> listOfFigures)
        {
            bool isOnField = false;
            for (int i = 0; i < listOfFigures.Count; i++)
            {
                if (listOfFigures[i].XNode >= field.StartX && listOfFigures[i].XNode <= field.StartX + field.Width - 1
                && listOfFigures[i].YNode >= field.StartY && listOfFigures[i].YNode <= field.StartY + field.Height - 1)
                {
                    isOnField = true;
                }
            }
            
            return isOnField;
        }

        public static bool IsOnFinishedList(List<Node> golist, List<Node> finlist)
        {
            bool isOnFinished = false;
            for (int i = 0; i < golist.Count; i++)
            {
                for (int j = 0; j < finlist.Count; j++)
                {
                    if (golist[i].XNode == finlist[j].XNode && golist[i].YNode + 1 == finlist[j].YNode)
                    {
                        isOnFinished = true;
                    }
                }
            }
            return isOnFinished;
        }

        public static bool IsOnFinishedListLeftSideMoving(List<Node> golist, List<Node> finlist)
        {
            bool isOnFinished = false;
            for (int i = 0; i < golist.Count; i++)
            {
                for (int j = 0; j < finlist.Count; j++)
                {
                    if (golist[i].XNode - 1 == finlist[j].XNode && golist[i].YNode == finlist[j].YNode)
                    {
                        isOnFinished = true;
                    }
                }
            }
            return isOnFinished;
        }

        public static bool IsOnFinishedListRightSideMoving(List<Node> golist, List<Node> finlist)
        {
            bool isOnFinished = false;
            for (int i = 0; i < golist.Count; i++)
            {
                for (int j = 0; j < finlist.Count; j++)
                {
                    if (golist[i].XNode + 1 == finlist[j].XNode && golist[i].YNode == finlist[j].YNode)
                    {
                        isOnFinished = true;
                    }
                }
            }
            return isOnFinished;
        }

        public static bool IsOnDown(GameField field, List<Node> list)
        {
            bool isOnDown = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].YNode + 1 >= field.StartY + field.Height)
                {
                    isOnDown = true;
                }
            }
            return isOnDown;
        }

        public static bool IsOnRight(GameField field, List<Node> list)
        {
            bool isOnRight = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].XNode + 1 >= field.StartX + field.Width)
                {
                    isOnRight = true;
                }
            }
            return isOnRight;
        }

        public static bool IsOnLeft(GameField field, List<Node> list)
        {
            bool isOnLeft = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].XNode - 1 <= field.StartX - 1)
                {
                    isOnLeft = true;
                }
            }
            return isOnLeft;
        }

        public static int IsLineComplete(List<Node> list, GameField field)
        {
            int duplicates = 0;
            int completeLines = 0;
            for (int j = field.Height; j >= 1; j--)
            {
                duplicates = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].YNode == j)
                    {
                        duplicates++;
                    }
                }
                if (duplicates == field.Width)
                {
                    completeLines++;
                }
            }
            return completeLines;

        }

        public static List<int> CoordYCompleteLine(List<Node> list, GameField field)
        {
            int duplicates = 0;
            List<int> duplictesYcoord = new List<int>();
            for (int j = field.Height; j >= 1; j--)
            {
                duplicates = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].YNode == j)
                    {
                        duplicates++;
                    }
                }
                if (duplicates == field.Width)
                {
                    duplictesYcoord.Add(j);
                }
            }
            return duplictesYcoord;
        }

        public static bool isRotateAvailable(CurrentFigure currentFigure, Figures figure, GameField field)
        {
            bool isRotateOK = true;
            if (currentFigure == CurrentFigure.Horse || currentFigure == CurrentFigure.Twos || currentFigure == CurrentFigure.Piptik)
            {
                if (figure.Node3.XNode <= field.StartX || figure.Node3.XNode >= field.StartX + field.Width - 1)
                {
                    isRotateOK = false;
                }
            }

            else if (currentFigure == CurrentFigure.Stick)
            {
                if (figure.Node3.XNode <= field.StartX + 1 || figure.Node3.XNode >= field.StartX + field.Width - 1)
                {
                    isRotateOK = false;
                }
            }

            return isRotateOK;
        }


    }
}
